package com.example.home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.HeaderViewListAdapter
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.example.common.cache.RecyclerCachePool
import com.example.common.entity.SimpleVideoEntity
import com.example.home.cache.HomeItemCache
import com.example.home.databinding.ItemVideoLayoutBinding
import com.example.image.GlideUtils
import com.example.image.glide.GlideImgLoader
import kotlin.math.max

/**
 * 适配器
 */
class HomeVideoListAdapter constructor(private val list:MutableList<SimpleVideoEntity>) :RecyclerView.Adapter<HomeVideoListAdapter.ItemViewHolder>(){

    private var scrollState=RecyclerView.SCROLL_STATE_IDLE//滚动停止
    var preloadEndCount=0//倒数第几个
    private var isPreloading=false//是否加载中
    var preloadMoreData:(()->Unit)? =null//回调方法
    var itemViewCache:HomeItemCache ?=null

    private var itemClickListener: OnItemClickListener ?= null

    fun setOnItemClickListener(l: OnItemClickListener){
        itemClickListener = l
    }

    inner class ItemViewHolder(val dataBinding: ItemVideoLayoutBinding) : RecyclerView.ViewHolder(dataBinding.root)
    //点击事件接口
    interface OnItemClickListener{
        fun onItemChick(v: View, position: Int)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        var itemViewHolder=ItemViewHolder(ItemVideoLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false))
        itemViewCache?.addViewHolder(viewType,itemViewHolder)
        return itemViewHolder
    }

    override fun getItemViewType(position: Int): Int {
        return RecyclerCachePool.HOME_LIST_ITEM_VIEW_TYPE
    }

    override fun getItemId(position: Int): Long =position.toLong()
    override fun getItemCount(): Int =list.size

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity =list[position]
        holder.dataBinding.tvTitle.text =entity.title
        holder.dataBinding.tvDesc.text=entity.description
        GlideUtils.loadCornerImage(holder.dataBinding.root.context,entity.videomainimag,holder.dataBinding.ivVideoThumbnail,10)
        GlideUtils.loadCircleImage(holder.dataBinding.root.context,entity.avatar_url,holder.dataBinding.ivAvatar)
        //播放按钮点击事件
        holder.dataBinding.ivVideoPlay.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        //评论按钮点击事件
        holder.dataBinding.ivCommend.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        //更多按钮点击事件
        holder.dataBinding.ivMore.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        //检查是否需要加载下一页数据
        checkPreloadNextData(position)
    }
    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        itemViewCache?.clearcache()
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        if (itemViewCache!=null){
            recyclerView.setViewCacheExtension(itemViewCache)
        }
        recyclerView.addOnScrollListener (object : RecyclerView.OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                scrollState=newState
                super.onScrollStateChanged(recyclerView, newState)
            }

        })
    }

    /**
     * 预加载
     */
    private fun checkPreloadNextData(position: Int) {
        if (scrollState!=RecyclerView.SCROLL_STATE_IDLE//滚动中触发
            && position== max(list.size-1-preloadEndCount,0)//当前页面最后几个
            && !isPreloading){
            //正在触发加载数据标识
            isPreloading=true
            //执行回调方法
            preloadMoreData?.invoke()
        }else{
            isPreloading=false
        }
    }


}